//
//  HMStatusPictureView.swift
//  weibo1
//
//  Created by 代亚洲 on 16/4/22.
//  Copyright © 2016年 itcast. All rights reserved.
//

import UIKit


/// 每一个条目之间的间距
let HMStatusPictureItemMargin: CGFloat = 5
/// 每一个条目的宽高 (collectionView item 的宽高，尽量设置成整数)
let HMStatusPictureItemWH: CGFloat = {
    // 转成Int                     cell里面公共的间距
    let intW = Int((SCREENW - 2 * HMStatusCellMargin - 2 * HMStatusPictureItemMargin) / 3)
    // 转成 Float
    let floatW = CGFloat(intW)
    return floatW
}()
class HMStatusPictureView: UICollectionView {
//cell 的重用ID
    private let pictureViewCellId = "pictureViewCellId"

////     当前配图控件显示的配图数据
//        var pic_urls: [HMStatusPhontoInfo]? {
//            didSet{
//                label.text = "\(pic_urls?.count ?? 0)"
//    
//                // 刷新数据
//                reloadData()
//            }
//        }
//    
    // 元组类型
    var dataInfo: (pic_urls: [HMStatusPhontoInfo], size: CGSize)? {
        didSet{
            
            if dataInfo?.pic_urls.count == 1 {
                let layout = self.collectionViewLayout as! UICollectionViewFlowLayout
                layout.itemSize = dataInfo?.size ?? CGSizeZero
            }else{
                let layout = self.collectionViewLayout as! UICollectionViewFlowLayout
                layout.itemSize = CGSize(width: HMStatusPictureItemWH, height: HMStatusPictureItemWH)
            }
            
            self.snp_updateConstraints { (make) -> Void in
                make.size.equalTo(dataInfo?.size ?? CGSizeZero)
            }
            
            reloadData()
            
        }
    }

    
    override init(frame: CGRect, collectionViewLayout layout: UICollectionViewLayout) {
        super.init(frame: frame, collectionViewLayout: layout)
        
        setUPUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func setUPUI(){
//        设置背景颜色
        backgroundColor = UIColor.orangeColor()
        
        
        // 注册cell
        registerClass(HMStatusPictureViewCell.self, forCellWithReuseIdentifier: pictureViewCellId)
        
//        设置每一条数目的大小
        let layout = self.collectionViewLayout as! UICollectionViewFlowLayout
        layout.itemSize = CGSize(width: HMStatusPictureItemWH, height: HMStatusPictureItemWH)
//        item之间的间距
        layout.minimumInteritemSpacing = HMStatusPictureItemMargin
        layout.minimumLineSpacing = HMStatusPictureItemMargin
        
        // 在这种情况下，因为当前控件是一个collectionView，而当前控件持有数据，所以就可以将数据源设置成自己
       self.dataSource = self
//        添加label
        addSubview(label)
//        约束
        label.snp_makeConstraints { (make) -> Void in
//            label的中心点等于self的中心点
            make.center.equalTo(self)
        }
    }


    // MARK: - 懒加载控件
    
    // 用于显示当前配图视图有多少张图片的
    private lazy var label: UILabel = UILabel(textColor: UIColor.darkGrayColor(), fontSize: 35)
}

// MARK: - UICollectionViewDataSource 数据源

extension HMStatusPictureView:UICollectionViewDataSource{
    
    func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier(pictureViewCellId, forIndexPath: indexPath) as!HMStatusPictureViewCell
//        背景色是随机颜色
        cell.backgroundColor = RandomColor
//       只有赋值 了才会调用cell的赋值方法   给 cell 设置图片数据
        cell.photoInFo = dataInfo!.pic_urls[indexPath.item]
        return cell
    }
    
    func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return dataInfo?.pic_urls.count ?? 0
    }
    
}

/**
*  自定义cell
*/

class HMStatusPictureViewCell: UICollectionViewCell {
    
//    提供给外界设置图片数据的属性
    var  photoInFo :HMStatusPhontoInfo? {

        didSet {
//            取得 imageView数据
            imageView.sd_setImageWithURL(NSURL(string: photoInFo?.thumbnail_pic ?? ""), placeholderImage: UIImage(named: "timeline_image_placeholder"))
            
//            print("====\(photoInFo?.thumbnail_pic)")
//        如果imageView的地址是以GIF结尾的话 就显示gif图标
            if  let url = photoInFo?.thumbnail_pic where url.hasSuffix(".gif"){
                gifImageView.hidden = false
            }else{
                gifImageView.hidden = true
            }
        }
        
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    func setupUI(){
        // 1. 添加视图
        contentView.addSubview(imageView)
        contentView.addSubview(gifImageView)
        
//        约束
        
        imageView.snp_makeConstraints { (make) -> Void in
//            imageview的边缘和contentView的边缘一致
            make.edges.equalTo(contentView)
        }
        gifImageView.snp_makeConstraints { (make) -> Void in
//           gifImageView在imageview右边缘的底部
            make.trailing.bottom.equalTo(imageView)
        }
//
    }
    
    // MARK: - 懒加载控件
    
    private lazy var imageView: UIImageView = {
        let imageView = UIImageView(image: UIImage(named: "timeline_image_placeholder"))
        // 设置内容模式
        imageView.contentMode = .ScaleAspectFill
        // 把超出部分给干掉
        imageView.clipsToBounds = true
       
//      print(imageView.image)
        return imageView
        
    }()
    
    // gif
    private lazy var gifImageView = UIImageView(image: UIImage(named: "timeline_image_gif"))
}













